package com.example.renttest;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.example.renttest.util.OkHttpUtil;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.FormBody;

public class RegisterActivity extends AppCompatActivity implements OnClickListener {

	private static final String TAG = "";
	private EditText userName_et,passWord_et,rePassword_et,email_et;
	private Button commit_btn;
	private int basicCount = 1000;
	private int userId;
	private Handler hand = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.arg2 == 2){
				Toast.makeText(RegisterActivity.this, "用户已存在", Toast.LENGTH_SHORT).show();
			}
			if(msg.arg2 == 3){
				Toast.makeText(RegisterActivity.this, "密码确认错误", Toast.LENGTH_SHORT).show();
			}
			if(msg.arg2 == 4){
				Toast.makeText(RegisterActivity.this, "请您填写完整！", Toast.LENGTH_SHORT).show();
			}
			if(msg.arg2 == 5){
				Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
			}
			if(msg.arg2 == 6){
				Toast.makeText(RegisterActivity.this, "注册失败,请重新注册", Toast.LENGTH_SHORT).show();
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initView();
	}
	private void initView(){
		userName_et = (EditText) findViewById(R.id.register_username);
		passWord_et = (EditText) findViewById(R.id.register_password);
		rePassword_et = (EditText) findViewById(R.id.register_repassword);
		email_et = (EditText) findViewById(R.id.register_email);
		commit_btn = (Button) findViewById(R.id.register_commit);
		commit_btn.setOnClickListener(this);
findViewById(R.id.tv_login).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();

			}
		});
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.register_commit:
			email_et.setText(userName_et.getText().toString());
			rePassword_et.setText(passWord_et.getText().toString());

			new Thread(){
				public void run() {
try{
					String register = register( userName_et.getText().toString(), passWord_et.getText().toString(), passWord_et.getText().toString() );
					Gson gson = new Gson();
					final StudentBean studentBean = gson.fromJson(register, StudentBean.class);

					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							if(!studentBean.isSuccess()){
								Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
							}else{
								Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
								finish();
							}
						}
					});

				} catch (IOException e) {
					e.printStackTrace();
				}

				};
			}.start();

			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}


	/**
	 * 注册的方法
	 * @param name
	 * @param phone
	 * @param password
	 */
	private String register(String name,  String phone, String password) throws IOException {
		String url = "http://"+getResources().getString(R.string.ip)+"/foodserver/user/registerFoParam";
		FormBody body = new FormBody.Builder()
				.add("name",name)
				.add("telephone",phone)
				.add("password",password)
				.build();
		String result = OkHttpUtil.postForParams(url, body);
		Log.e(TAG, "register: "+result);
		return result;
	}

}
